package ophiux.wechatpay.sdk.servlet;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 * 说明：微信授权<br>
 * <h1>创 建 人: 何海龙   <br></h1>
 * 创建日期: 2017年7月17日上午09:32:28<br>
 * 需要的jar包:servlet-api.jar
 */
public class OauthServlet extends HttpServlet{
	private String REDIRECT_URL = "http://wechatpay.ophiux.com/OphiuxWeChatPayPlatformbuild/RedirectServlet";
	//192.168.101.179:8087/InterfaceUtils/aouth?appid=wx84f6558264b003c9&appsecret=5de9fdd1fe6a9da5673f0eed525f2063
//	private String REDIRECT_URL = "http://wechathhls.applinzi.com/redirect";
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String appid = request.getParameter("appid");
		String appsecret = request.getParameter("appsecret");

		String client_back_url = "http://www.baidu.com";// request.getParameter("client_back_url");

		String backUri = this.REDIRECT_URL;

		backUri = backUri + "?appid=" + appid + "&appsecret=" + appsecret
				+ "&client_back_url=" + client_back_url;
		System.out.println("回调地址url="+backUri);
		backUri = URLEncoder.encode(backUri);
		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ appid
				+ "&redirect_uri=" + backUri
				+ "&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
		System.out.println("请求微信url=" + url);

		response.sendRedirect(url);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
